<template>
  <div class="app-container">
    <div v-permission="['subcontractor:create']" class="createManager">
      <router-link to="createSubcontractor">
        <div style="margin: 0 0 0 -5px;" class="addbtn">创建分包商</div>
      </router-link>
    </div>
    <div class="seatch">
      <el-input
        v-model="listQuery.keyword"
        placeholder="按名称搜索"
        @keyup.enter.native="searchName($event)"
      >
        <i slot="prefix" class="el-input__icon el-icon-search" @click="getList()" />
      </el-input>
    </div>
    <div class="content">
      <el-table
        v-loading="listLoading"
        align="center"
        :data="tableData"
        style="width: 100%"
        @filter-change="handleFilterChange"
      >
        <el-table-column
          prop="name"
          align="center"
          label="分包商名称"
        />
        <el-table-column
          prop="type"
          align="center"
          label="分包商类型"
          :filter-multiple="false"
          :column-key="'type'"
          :filters="statueCopyList"
          filter-placement="bottom-end"
        >
          <template slot-scope="{row}">
            <div>
              <div>
                <span>{{ row.typeName }}</span>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="contactsName"
          label="负责人"
        />
        <el-table-column
          align="center"
          prop="contactsPhone"
          label="负责人电话"
        />
        <el-table-column
          align="center"
          prop="hascontract"
          label="合同附件"
        >
          <template slot-scope="{row}">
            <div>
              <div>
                <span>{{ row.hascontract?'是':'否' }}</span>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="money"
          label="已付总金额(元)"
        />
        <el-table-column
          align="center"
          label="操作"
        >
          <template slot-scope="{row}">
            <div class="operate">
              <div>
                <router-link :to="{ path: 'detailSubcontractor', query: { id: row.id }}">详情</router-link>
              </div>
              <div v-permission="['subcontractor:edit']">
                <router-link :to="{ path: 'edgeSubcontractor', query: { id: row.id }}">编辑</router-link>
              </div>
              <div v-permission="['subcontractor:delete']" @click="deletePatrol(row)">
                删除
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pageination-div">
        <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="getList" />
      </div>

    </div>
    <delete-dialog
      dislog-name="分包商"
      :dialog-del-visible="dialogDelVisible"
      :title="'分包商'"
      @funcancl="funcancl"
      @func="deleteData"
    />
  </div>

</template>

<script>
import { Message } from 'element-ui'
import Pagination from '@/components/pagination'
import { subcontractorList, deleteSubcontractor, getSubcontractorType } from '@/api/subcontractor'
import DeleteDialog from '@/components/dialog/delete'
export default {
  filters: {
    statusTypeFilter(status) {
      console.log(status)
      // this.statueList
      // return this.statueList[status]
    }
  },
  components: {
    Pagination,
    DeleteDialog
  },
  data() {
    return {
      tableData: [],
      total: 0,
      listLoading: true,
      dialogDelVisible: false,
      teamDetail: {},
      listQuery: {
        keyword: null,
        type: null,
        pageNum: 1,
        pageSize: 10
      },
      statueList: [],
      statueCopyList: [],
      statusObject: {}
    }
  },
  beforeMount() {
    this.subcontractorStatus()
  },
  mounted() {
    this.getList()
  },
  methods: {
    sizeChange(val) {
      this.pageNum = val
    },
    currentChange(val) {
      this.currentPage = val
    },
    searchName(e) {
      this.listQuery.keyword = e.target.value
      this.getList()
    },
    // 获取类型
    async subcontractorStatus() {
      const res = await getSubcontractorType()
      if (res.code === 200) {
        this.statueList = res.data
        if (this.statueList) {
          for (let i = 0; i < this.statueList.length; i++) {
            this.statueCopyList.push({ value: this.statueList[i].id, text: this.statueList[i].name })
            // this.statusObject = Object.assign(this.statusObject, { value: this.statueList[i].type, text: this.statueList[i].name })
          }
        }
      }
    },
    handleFilterChange(filters) {
      this.listQuery.type = filters.type[0]
      this.getList()
    },
    // 获取数据
    async getList() {
      const res = await subcontractorList(this.listQuery)
      this.listLoading = false
      if (res.code === 200) {
        this.tableData = res.data.list
        this.total = res.data.total || 0
        this.pages = res.data.pages || 0
      } else {
        Message({
          message: res.data.msg || 'error',
          type: 'error',
          showClose: true,
          duration: 2 * 1000
        })
      }
    },
    deletePatrol(data) {
      this.dialogDelVisible = true
      this.teamDetail = data
    },
    // 删除
    async deleteData(val) {
      const id = this.teamDetail.id
      const res = await deleteSubcontractor(id)
      if (res.code === 200) {
        this.dialogDelVisible = false
        this.getList()
        this.$message({
          type: 'success',
          message: '删除成功!',
          showClose: true,
          center: true,
          duration: 2 * 1000
        })
      } else {
        this.dialogDelVisible = false
      }
    },
    funcancl(val) {
      this.dialogDelVisible = false
    }

  }
}
</script>
<style lang="scss" scoped>
@import '../../styles/variables';
.app-container{
  .createManager{
    position: absolute;
    margin: 13px 0 0 26px;
    font-size:$fontSize14;
    font-weight:400;
    color:rgba(255,255,255,0.65);
   .down{
      margin: 0 0 0 10px;
      background:linear-gradient(90deg,rgba(40,207,255,0.4) 0%,rgba(14,154,221,0.4) 100%);
   }
  }
  .content{
    .operate{
      // display: flex;
      font-size:$fontSize12;
      font-weight:400;
      color:rgba(247,181,0,1);
      div{
        cursor: pointer;
        // flex: 1;
      }
    }
  }
}
</style>
